package com.wellsoft.mobileoa.utils.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.os.AsyncTask;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.dbmodel.ContactInfo;
import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_Dept;
import com.wellsoft.mobileoa.wsmodel.WSOA_DownloadUnit_Request;
import com.wellsoft.mobileoa.wsmodel.WSOA_DownloadUnit_Response;
import com.wellsoft.mobileoa.wsmodel.WSOA_Page;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListItem;
import com.wmobile.utils.BaseObject;
import com.wmobile.utils.PopConfirmDialog;
import com.wmobile.utils.ServiceResponse;

public class FunctionView extends BaseView {

	public ListView funcLv;
	public FunctionViewAdapter funcAdp;

	public FunctionView(BaseActivity baseActivity, WSOA_Page page, String url) {
		super(baseActivity);
		this.baseActivity = baseActivity;
		this.page = page;
		this.url = url;
	}

	@Override
	public ViewGroup DrawSelf() {
		if (page.getPageListSet().getListItems() == null) {
			return null;
		}
		funcAdp = new FunctionViewAdapter(page.getPageListSet(), baseActivity);
		selfView = (ViewGroup) LayoutInflater.from(baseActivity).inflate(
				R.layout.functionview, null);

		initTitleBar(selfView);

		funcLv = (ListView) selfView.findViewById(R.id.functionview_funclv);

		funcLv.setOnItemClickListener(listViewOnItemClickListener);

		funcLv.setAdapter(funcAdp);

		initToolBar(selfView);
		return selfView;

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			backOnClickListener.onClick(null);
		}

		return super.onKeyDown(keyCode, event);
	}

	OnItemClickListener listViewOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			synchronized (FunctionView.this.isExecuting) {
				if (isExecuting) {
					return;
				}
				isExecuting = true;
				WSOA_PageListItem listItem = page.getPageListSet()
						.getListItems().get(arg2);

				if (listItem.getAction().getPageConfirm() != null
						&& !SystemConfig.stringIsNullOrEmpty(listItem
								.getAction().getPageConfirm().getMessage())) {
					PopConfirmDialog confirmDialog = new PopConfirmDialog(
							FunctionView.this.baseActivity, listItem
									.getAction().getPageConfirm().getMessage());
					if (confirmDialog.showDialog() != PopConfirmDialog.DialogResultOK) {
						isExecuting = false;
						return;
					}
				}

				new AsyncTask<WSOA_PageListItem, Integer, ServiceResponse>() {
					AlertDialog alertDialog;

					protected ServiceResponse doInBackground(
							WSOA_PageListItem... params) {
						WSOA_PageListItem listItem = params[0];
						return BaseAction.executeActions(
								listItem.getActionID(), listItem.getAction(),
								FunctionView.this, true);
					}

					@Override
					protected void onPreExecute() {
						alertDialog = baseActivity.showWaitingDialog();
					}

					@Override
					protected void onPostExecute(ServiceResponse response) {
						alertDialog.dismiss();
						isExecuting = false;
						if (!response.getExeFlag()) {
							baseActivity.showMessage(response.getMessage());
							return;
						}
						if (!SystemConfig.stringIsNullOrEmpty(response
								.getMessage())
								&& response.getMessage().equals("同步成功")) {
							baseActivity.showMessage(response.getMessage());
							return;
						}
					}

				}.execute(listItem);
				// if (isExecuting) {
				// return;
				// }
				// isExecuting = true;
				// AlertDialog alertDialog = baseActivity.showWaitingDialog();
				// WSOA_PageListItem listItem = page.getPageListSet()
				// .getListItems().get(arg2);
				// ServiceResponse response = BaseAction.executeActions(
				// listItem.getActionID(), listItem.getAction(),
				// FunctionView.this);
				// alertDialog.dismiss();
				// isExecuting = false;
				// if (!response.getExeFlag()) {
				// baseActivity.showMessage(response.getMessage());
				// return;
				// }
			}
		}
	};
}
